//
//  JEHNewsTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/1.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHNewsTableViewCell.h"

@interface JEHNewsTableViewCell ()

@property (nonatomic, retain) UILabel *detailLabel;
@property (nonatomic, retain) UILabel *replyLabel;
@property (nonatomic, assign) NSInteger flag;
@property (nonatomic, retain) UIImageView *leftImage;

@end

@implementation JEHNewsTableViewCell

- (void)dealloc {
    
    [_rootModel release];
    [_leftImage release];
    [_titleLabel release];
    [_detailLabel release];
    [_replyLabel release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _leftImage.frame = CGRectMake(10, 15, (self.width - 40) / 3, self.height - 30);
    _titleLabel.frame = CGRectMake(_leftImage.right + 10, 15, _leftImage.width * 2, (self.height - 20) / 3);
    _detailLabel.frame = CGRectMake(_leftImage.right + 10, _titleLabel.height + 8, _titleLabel.width, _titleLabel.height * 2);
    CGFloat replyWidth = [self getWidthWithText:self.replyLabel.text withFont:10];
    CGFloat replyHeight = [self getHeightWithText:self.replyLabel.text withFont:10];
    _replyLabel.frame = CGRectMake(self.right - replyWidth - 10, self.height - 20, replyWidth, replyHeight);
}

- (CGFloat)getWidthWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}

- (CGFloat)getHeightWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(0, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)setRootModel:(JEHRootModel *)rootModel {
    if (_rootModel != rootModel) {
        [_rootModel release];
        _rootModel = [rootModel retain];
    }
    [_leftImage sd_setImageWithURL:[NSURL URLWithString:rootModel.kpic] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    _titleLabel.text = rootModel.title;
    _detailLabel.text = rootModel.intro;
    if ([rootModel.category isEqualToString:@"sponsor"]) {
        _replyLabel.text = @"赞助";
        _replyLabel.layer.borderWidth = 0.4;
        _replyLabel.textColor = [UIColor orangeColor];
        _replyLabel.layer.borderColor = [UIColor orangeColor].CGColor;
    } else if ([rootModel.category isEqualToString:@"video"]) {
        _replyLabel.text = [NSString stringWithFormat:@"%ld评论 🔞", (long)rootModel.commentModel.total];
        _replyLabel.layer.borderWidth = 0;
        _replyLabel.textColor = [UIColor grayColor];
        _replyLabel.layer.borderColor = [UIColor clearColor].CGColor;
    } else {
        _replyLabel.text = [NSString stringWithFormat:@"%ld评论", (long)rootModel.commentModel.total];
        _replyLabel.layer.borderWidth = 0;
        _replyLabel.textColor = [UIColor grayColor];
        _replyLabel.layer.borderColor = [UIColor clearColor].CGColor;
    }
}

- (void)createView {
    
    self.leftImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_leftImage release];
    _leftImage.contentMode = UIViewContentModeScaleAspectFill;
    _leftImage.clipsToBounds = YES;
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_titleLabel release];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    
    self.detailLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_detailLabel release];
    self.detailLabel.font = [UIFont systemFontOfSize:12];
    self.detailLabel.textColor = [UIColor grayColor];
    self.detailLabel.numberOfLines = 0;
    
    self.replyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_replyLabel release];
    self.replyLabel.font = [UIFont systemFontOfSize:10];
    self.replyLabel.textColor = [UIColor grayColor];
    self.replyLabel.textAlignment = NSTextAlignmentRight;
    
    [self.contentView addSubview:_leftImage];
    [self addSubview:_titleLabel];
    [self addSubview:_detailLabel];
    [self.contentView addSubview:_replyLabel];
    
#pragma mark - NightMode
    NSNotificationCenter *centers = [NSNotificationCenter defaultCenter];
    [centers addObserver:self selector:@selector(nightMode:) name:@"NightMode" object:nil];
}

- (void)nightMode:(NSNotification *)notification {
    
    if ([notification.object isEqualToString:@"YES"]) {
        
        self.leftImage.alpha = 0.7;
        self.titleLabel.textColor = [UIColor whiteColor];
    } else {
        self.leftImage.alpha = 1;
        self.titleLabel.textColor = [UIColor blackColor];
    }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
